﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;


using System.Threading;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;
using CCWin;
namespace weiqijiance
{
    public partial class TongDaoSheZhi : CCSkinMain
    {
        // /声明INI文件的写操作函数 WritePrivateProfileString()
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //声明INI文件的读操作函数 GetPrivateProfileString()
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        private string sPath = null;


        public mainform m_mainform;// { get; set; }
        public virtual void setmainform(mainform m_m)
        {
            m_mainform = m_m;
        }
        public TongDaoSheZhi()
        {
            InitializeComponent();
            
            readshezhi();
            readshezhiSQL();
            readshezhiGW();
           

        
        }

        //ini文件
        public void Ini(string path)
        {
            this.sPath = path;
        }
        public void Writue(string section, string key, string value)
        {
            // section=配置节，key=键名，value=键值，path=路径
            WritePrivateProfileString(section, key, value, sPath);
        }
        public string ReadValue(string section, string key)
        {
            // 每次从ini中读取多少字节
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);

            // section=配置节，key=键名，temp=上面，path=路径
            GetPrivateProfileString(section, key, "", temp, 255, sPath);
            return temp.ToString();
        }
        //ini文件
        private void but_BaoCun_Click(object sender, EventArgs e)
        {
            
        }
        public void baocunshezhi()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\串口及通道设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

           
            Writue("串口设置", "变频器", text_BPQ.Text);
            Writue("串口设置", "废气仪", text_GAS.Text);
            Writue("串口设置", "废气仪厂家", com_GAS.Text);

            Writue("串口设置", "不透光仪", text_SK.Text);
            Writue("串口设置", "不透光厂家", com_SK.Text);
            Writue("串口设置", "转速仪", text_ZS.Text);
            Writue("串口设置", "转速仪厂家", com_zsycj.Text);
            //Writue("串口设置", "LED灯牌", text_DP.Text);
            //Writue("串口设置", "LED灯牌2", text_DP2.Text);
            //Writue("串口设置", "LED灯牌厂家", com_dpcj.Text);
            //Writue("串口设置", "灯牌厂家", com_dpcj.Text);
            Writue("串口设置", "环境温湿度", text_WD.Text);
            Writue("串口设置", "环境温湿度厂家", com_wdcj.Text);
            //Writue("串口设置", "OBD设备", text_OBD.Text);

           

            iniclass objini = new iniclass();
            基类窗体.UploadAndDownload fupload = new 基类窗体.UploadAndDownload();
            objini.Ini(System.Windows.Forms.Application.StartupPath + "\\系统信息.ini");
            string sjcxname = objini.ReadValue("系统信息", "检测线名称");
            string sgongweiname = objini.ReadValue("系统信息", "检测工位名称");
            fupload.upload(sjcxname, sgongweiname, "串口及通道设置.ini");

        }
        public void baocunshezhiSQL()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\数据库设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

            Writue("设置", "数据库IP", text_sjkip.Text);
            Writue("设置", "数据库名称", text_sjkmc.Text);
            Writue("设置", "用户", text_yonghu.Text);
            Writue("设置", "密码", text_mima.Text);


            Writue("尾气设置", "数据库IP", text_sjkip2.Text);
            Writue("尾气设置", "数据库名称", text_sjkmc2.Text);
            Writue("尾气设置", "用户", text_yonghu2.Text);
            Writue("尾气设置", "密码", text_mima2.Text);
            
        }
        public void baocunshezhiLW()
        {
           
            


        }
        public void baocunshezhiJD()
        {
           
           



        }
        public void baocunshezhiJC()
        {
           


            
           




        }
        public void readshezhi()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\串口及通道设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);
           
           
            text_BPQ.Text=ReadValue("串口设置", "变频器");
            text_GAS.Text= ReadValue("串口设置", "废气仪");
            com_GAS.Text = ReadValue("串口设置", "废气仪厂家");

           
            text_SK.Text = ReadValue("串口设置", "不透光仪");
            com_SK.Text = ReadValue("串口设置", "不透光厂家");
            text_ZS.Text= ReadValue("串口设置", "转速仪");
            com_zsycj.Text = ReadValue("串口设置", "转速仪厂家");

            //text_DP.Text = ReadValue("串口设置", "LED灯牌");
            //com_dpcj.Text = ReadValue("串口设置", "LED灯牌厂家");
            //text_DP2.Text = ReadValue("串口设置", "LED灯牌2");

            text_WD.Text = ReadValue("串口设置", "环境温湿度");
            com_wdcj.Text = ReadValue("串口设置", "环境温湿度厂家");



           


        }
        public void readshezhiSQL()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\数据库设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

            text_sjkip.Text = ReadValue("设置", "数据库IP");
            text_sjkmc.Text = ReadValue("设置", "数据库名称");
            text_yonghu.Text = ReadValue("设置", "用户");
            text_mima.Text = ReadValue("设置", "密码");

            text_sjkip2.Text = ReadValue("尾气设置", "数据库IP");
            text_sjkmc2.Text = ReadValue("尾气设置", "数据库名称");
            text_yonghu2.Text = ReadValue("尾气设置", "用户");
            text_mima2.Text = ReadValue("尾气设置", "密码");
        }



      

        private void skinBut_speed_Click(object sender, EventArgs e)
        {
            xinhaochulibanxinzeng obj = new xinhaochulibanxinzeng();

            obj.ShowDialog();
            readshezhi();//刷新设置
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            shebeixinxi obj = new shebeixinxi();

            obj.setmainform(m_mainform);
            obj.ShowDialog();
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            try
            {
                if (!CheckData())
                {
                    if (MessageBox.Show("是否继续设置？", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        baocunshezhiSQL();
                        baocunshezhi();
                        baocunshezhiLW();
                        baocunshezhiJD();
                        baocunshezhiJC();
                        baocunshezhiGW();
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    baocunshezhiSQL();
                    baocunshezhi();
                    baocunshezhiLW();
                    baocunshezhiJD();
                    baocunshezhiJC();
                    baocunshezhiGW();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public bool CheckData()
        {
            bool flag = true;
            if (!(text_sjkip.Text == "."))
            {
                if (!m_mainform.IsIp(text_sjkip.Text))
                {
                    MessageBox.Show("IP地址不正确");
                    flag = false;
                }
            }
            return flag;
        }

        private void skinButton3_Click(object sender, EventArgs e)
        {
           
            gwpz obj = new gwpz();
            obj.ShowDialog();
        }

        private void skinButton4_Click(object sender, EventArgs e)
        {
            jiancecanshushezhi obj = new jiancecanshushezhi();
            obj.ShowDialog();
        }

        private void skinButton5_Click(object sender, EventArgs e)
        {
            基类窗体.UploadAndDownload obj = new 基类窗体.UploadAndDownload();
            obj.sjcx = txtjcx.Text;
            obj.sgongwei = txtgongwei.Text;
            obj.ShowDialog();
        }
        public void baocunshezhiGW()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\系统信息.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);
            Writue("系统信息", "检测线名称", txtjcx.Text);
            Writue("系统信息", "检测工位名称", txtgongwei.Text);
        }
        public void readshezhiGW()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\系统信息.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

            txtjcx.Text = ReadValue("系统信息", "检测线名称");
            txtgongwei.Text = ReadValue("系统信息", "检测工位名称");
        }

        private void TongDaoSheZhi_Load(object sender, EventArgs e)
        {

        }

        bool bkeydown = false;
        private void TongDaoSheZhi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F8)
            {
                bkeydown = true;
            }
        }

        private void TongDaoSheZhi_KeyUp(object sender, KeyEventArgs e)
        {

            bkeydown = false;
        }

        private void TongDaoSheZhi_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && bkeydown)// && KeyDown ==Keys.Shift )
            {
                Point p = new Point(e.X, e.Y);
                contextMenuStrip1.Show(p);
                //skinButton4.Visible = true;
                //keybool = false;
                bkeydown = false;
            }
        }

        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            jiancecanshushezhi obj = new jiancecanshushezhi();
            obj.ShowDialog();
        }

    }
}
